{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7d67b000-7247-49ce-ae2c-32c324ac0576",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 32.DS1307实时时钟模块实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：32.DS1307实时时钟模块实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：32.DS1307实时时钟模块实验\n",
    "DS1307实时时钟模块实验，DS1307库进行操作，进行I2C控制方式，控制DS1307实时时钟模块，并在界面上设置和打印出对应的实时时间！！！！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab179391-9c46-48fd-92a4-d95e587c591a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import time\n",
    "import datetime\n",
    "import SDL_DS1307   # DS1307 实时时钟模块库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.初始化工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\" \")\n",
    "print(\"makerobo DS1307 Real-time clock Demo\")\n",
    "print(\" \")\n",
    "print(\" \")\n",
    "print(\"Program Started at:\"+ time.strftime(\"%Y-%m-%d %H:%M:%S\"))\n",
    "\n",
    "filename = time.strftime(\"%Y-%m-%d%H:%M:%SRTCTest\") + \".txt\"\n",
    "starttime = datetime.datetime.utcnow()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "677d8188-ab83-4095-83fd-96a8f0d9738a",
   "metadata": {},
   "source": [
    "## 3.DS1307 实时时钟I2C实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0aa85497-1401-414b-af07-6a88562437f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds1307 = SDL_DS1307.SDL_DS1307(1, 0x68)  # i2c地址为0x68\n",
    "ds1307.write_now()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e02fbeb2-59db-4f0c-bf5c-78f4e5bd8804",
   "metadata": {},
   "source": [
    "## 4.循环函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e77514af-5ab2-4e49-a633-90fd484dcca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "    while True:\n",
    "        currenttime = datetime.datetime.utcnow()\n",
    "        deltatime = currenttime - starttime\n",
    "        print(\" \")\n",
    "        print(\"Raspberry Pi=\\t\" + time.strftime(\"%Y-%m-%d %H:%M:%S\"))\n",
    "        print(\"DS1307 RTC =\\t20%d-%d-%d %d:%d:%d\" \n",
    "              % (ds1307._read_year(),ds1307._read_month(),\n",
    "                 ds1307._read_date(),ds1307._read_hours(),\n",
    "                 ds1307._read_minutes(),ds1307._read_seconds()))\n",
    "        time.sleep(10.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 5.程序入口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 程序入口\n",
    "if __name__ == \"__main__\":\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()       # 循环显示信息\n",
    "\texcept KeyboardInterrupt: # 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tprint(\"Exit\")  # Exit on CTRL+C"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
